var ctx;
var level = -1;
var state = -1;
var page = 1;
var postCode = "";
var regionName = "";
var pSize;
$("#province").hide();

function setCtx(ctx){
	this.ctx = ctx;
}

function ifNull(mess){
	if(mess == null || mess == undefined){
		mess = "";
	}
	return mess;
}

//分页跳转专业面
function gotoPage(page){
	regionName = ifNull($("#regionName").val());
	postCode = ifNull($("#postCode").val());
	level = ifNull($("#districtLevel").val());
	state = ifNull($("#state").val());
	pSize = ifNull($("#pSize").val());
	//解决ie9以下placeholder与value重复的问题
	if(postCode == $("#postCode").attr("placeholder")){
		postCode = "";
	}
	if(regionName == $("#regionName").attr("placeholder")){
		regionName = "";
	}
	loadContent("/district/districtList?pNo="+ page +"&postCode="+ postCode + "&regionName="+ regionName +"&level=" + level + "&state=" + state + "&pSize="+ pSize);
}

function gotoViewPage(page) {
	if(page == null){
		page = $(".inputclass").val();
	}
	regionName = ifNull($("#regionName").val());
	postCode = ifNull($("#postCode").val());
	level = ifNull($("#districtLevel").val());
	state = ifNull($("#state").val());
	pSize = ifNull($("#pSize").val());
	//解决ie9以下placeholder与value重复的问题
	if(postCode == $("#postCode").attr("placeholder")){
		postCode = "";
	}
	if(regionName == $("#regionName").attr("placeholder")){
		regionName = "";
	}
	loadContent("/district/districtList?pNo="+ page +"&postCode="+ postCode + "&regionName="+ regionName +"&level=" + level + "&state=" + state + "&pSize="+ pSize);
}

//条件搜索菜单列表
function districtList(){
	regionName = ifNull($("#regionName").val());
	postCode = ifNull($("#postCode").val());
	level = ifNull($("#districtLevel").val());
	state = ifNull($("#state").val());
	pSize = ifNull($("#pSize").val());
	//解决ie9以下placeholder与value重复的问题
	if(postCode == $("#postCode").attr("placeholder")){
		postCode = "";
	}
	if(regionName == $("#regionName").attr("placeholder")){
		regionName = "";
	}
	loadContent("/district/districtList?pNo="+ page +"&postCode="+ postCode + "&regionName="+ regionName +"&level=" + level + "&state=" + state + "&pSize="+ pSize);
}

function showEditLevel(){
	$("#editLevel").show();
}

//显示状态下拉框
function showState(){
	$("#state").show();
}

//选择状态
function setState(state){
	this.state = state;
	var stateText = "";
	
	if(state == 0){
		stateText = "启用";
	}else if(state == 1){
		stateText = "停用";
	}else{
		stateText = "全部";
	}
	
	$("#stateText").text(stateText);
	$("#state").hide();
}


//弹出新增或编辑页面
function saveOrUpdate (id) {
	if(id != null){
		
		var itemState = $("#itemState").val();
		
		if(itemState == 1){
			
			msg = "编辑区域";
			url =  ctx + "/district/selectById?id="+id+"";
			saveOrUpdateBox(msg,url);

		}else{
			layer.msg("当前区域处于停用状态，不能编辑！",{
			    icon: 3,
			    time: 2000 //2秒关闭（如果不配置，默认是3秒）
			});
		}
	}else{
		msg = "新增区域";
		url = ctx + "/district/selectById?id=0&level=-1";
		saveOrUpdateBox(msg,url);
	}
	
}

var index;
function saveOrUpdateBox(msg,url){
    //官网欢迎页
     index = layer.open({
        type: 2,
        //skin: 'layui-layer-lan',
        title: msg,
        fix: false,
        shadeClose: true,
        scrollbar: false,
        maxmin: false,
        edit: true,
        closeBtn: 1,
        area: ['500px', '320px'],
        content: url,
        btn: ['确定', '取消']
	    ,yes: function(index, layero){ //或者使用btn1
	        //按钮【按钮一】的回调
	    	var iframeWin = window[layero.find('iframe')[0]['name']]; 
	    	iframeWin.saveOrUpdateDistrict();


	    },cancel: function(index){ //或者使用btn2
	        //按钮【按钮二】的回调
	    }
	    });
}

//停用，启用区域方法
function toggleState(id,state){
	var mess = "";
	if(state == 1){
		mess ="请确认将要停用该区域!"; 
	}else{
		mess ="请确认将要启用该区域!"; 
	}
	 layer.confirm(mess, {icon: 3, title:'提示'}, function(index){
		    //do something
		   		$.get(ctx + "/district/updateState",{
					id: id,
					state: state	
		   		},function(data){
	   			data = JSON.parse(data);
				if(data.success){
					layer.msg("操作成功",{
					    icon: 1,
					    time: 2000 //2秒关闭（如果不配置，默认是3秒）
					},function(){
						loadContent("/district/districtList");
					});
				}else{
					layer.msg(data.msg,{
					    icon: 2,
					    time: 2000 //2秒关闭（如果不配置，默认是3秒）
					});
				}
			});
		    layer.close(index);
		}); 
}  

//全选和反选
function selectAll(target){    
    if(target.checked){    
        $("#tableTbody :checkbox").attr("checked", true); 
        $(target).parents("thead").siblings("tbody").children().children().each(function(){
    	    $(this).css("background","#D3F0F1");
       });
    }else{    
        $("#tableTbody :checkbox").attr("checked", false); 
        $(target).parents("thead").siblings("tbody").children().children().each(function(){
    	    $(this).css("background","");
       });
    }    

}

//鼠标滑入一行改变背景颜色
function mouseoverOne(target){
	 $(target).children().each(function(){
		  	    $(this).css("background","#D3F0F1");
		     });
}

//鼠标滑出一行恢复原来的背景颜色
function mouseoutOne(target){
	 $(target).children().each(function(){
	  	    $(this).css("background","");
	     });
}
//选择或取消选中当前记录
function selectOne(id,target){
    if(target.checked){    
        $("#locat" + id).attr("checked", true);  
        $(target).parents("tr").children().each(function(){
    	    $(this).css("background","#D3F0F1");
       });
    }else{    
        $("#locat" + id).attr("checked", false); 
        $(target).parents("tr").children().each(function(){
    	    $(this).css("background","");
       });
    } 
}


function deleteByIds(){
	if($("img[name='locatChecked'][src*=honggou]").length == 0){
		layer.msg("请选择要删除的记录!",{
			icon: 3,
			time: 2000 //2秒关闭（如果不配置，默认是3秒）
		});
	}else{
		var delIds = [];
		    $("#tableTbody img[name='locatChecked'][src*=honggou]").each(function(){ 
		    	delIds.push($(this).attr("class")); 
		    }); 
		    /*var vals = delIds.join(',');*///转换为逗号隔开的字符串 
			layer.confirm('确认删除?', {icon: 3, title:'提示'}, function(index){
			    //do something
				$.post(ctx+"/district/deleteByPrimaryKeys",{
					ids:delIds,
				},function(data){
					data = JSON.parse(data);
					if(data.success){
						layer.msg("删除成功",{
							icon: 1,
							time: 2000 //2秒关闭（如果不配置，默认是3秒）
						},function(){
							parent.loadContent("/district/districtList");
							parent.layer.close(index);
						});
					} else {
						layer.msg(data.msg,{
							icon: 2,
							time: 2000 //2秒关闭（如果不配置，默认是3秒）
						});
					}
				});
			    layer.close(index);
			});

		
	}
}
